# delimit ;

capture log close;
clear all;

global manyvars tratio pval sig fw ipr gpr monitor rival subs comps;
global onevars authors beta se_beta nobs lab nonlab year pub highskill field no_elast loglog diffdep;

insheet using "$bpath/peer_data.csv", comma names;
replace authors = subinstr(authors,"Thoni","Th"+char(246)+"ni",.);
replace authors = subinstr(authors,"Schonberg","Sch"+char(246)+"nberg",.);
replace authors = subinstr(authors,"Gachter","G"+char(228)+"chter",.);

/* CREATING DUMMY VARIABLES FROM FIELDS */
gen lab = (type=="Lab Experiment");
gen nonlab = ~lab;
gen field = (type=="Field Experiment");
drop type;

gen highskill = (skill=="High");
gen pub = (publication~="Working Paper");
gen tratio = abs(beta)/se_beta;
gen sig=tratio>=1.96;
gen pval = 2*normal(-tratio);

foreach var of varlist fw ipr gpr monitor rival {;
    gen `var'2 = (`var'=="Yes");
    drop `var';                                             
    rename `var'2 `var';
};
replace fw = fw==1 & ipr==0;

gen subs = (tech=="perfect substitutes");
gen comps = (tech=="complements");

replace diffdep = (diffdep==1);
replace loglog = (loglog==1);
replace no_elast = (no_elast==1);

gen pool = (pooled==1);
replace combine = (combine==1);
keep $manyvars $onevars pool combine sd_dep sd_ind mean_ind mean_dep;

/* CREATE ELASTICTIY BASED ESTIMATES FOR diffdep variables, where possible */
expand 2 if diffdep & ~no_elast, gen(elast);
replace beta = beta*(sd_dep/sd_ind)*(mean_ind/mean_dep) if elast;
replace se_beta = se_beta*(sd_dep/sd_ind)*abs(mean_ind/mean_dep) if elast;

/* COMBINING ESTIMATES FOR PAPER WHICH SEPARATE HIGHER V. LOWER */
gen S = se_beta if combine;
gen mbeta = beta if combine;

sort authors;
by authors: gen sumnobs = sum(nobs) if combine;
by authors: replace mbeta = (beta*nobs + mbeta[_n-1]*sumnobs[_n-1]) / sumnobs if _n>1 & combine;
by authors: replace S = sqrt( ((nobs-1)*S^2 + (sumnobs[_n-1]-1)*S[_n-1]^2 + ((nobs*sumnobs[_n-1])/(nobs+sumnobs[_n-1]) * (beta - mbeta[_n-1])^2 )) / (nobs + sumnobs[_n-1] - 1) ) if _n>1 & combine;

replace beta = mbeta if combine;
replace se_beta = S if combine;
replace nobs = sumnobs if combine;

by authors: drop if sum(combine)<_N & combine;

drop S mbeta sumnobs combine;

do make_labels;

preserve;

do metacode;

drop if pool;
drop pool;

drop if elast;
drop elast;

/* THIS IS THE SAMPLE-LEVEL DATABASE */
save "$bpath/manysample.dta", replace;
restore;


/* THIS IS COMBINING ACROSS SAMPLES TO GET TAU2 WITH PER-PAPER FIXED EFFECT */

gen S = se_beta;
gen mbeta = beta;

sort authors elast pool;
by authors elast: keep if pool == pool[_N];
by authors elast: gen numest = _N;
by authors elast: gen sumnobs = sum(nobs);

by authors elast: replace mbeta = (beta*nobs + mbeta[_n-1]*sumnobs[_n-1]) / sumnobs if _n>1;

by authors elast: replace S = sqrt( ((nobs-1)*S^2 + (sumnobs[_n-1]-1)*S[_n-1]^2 + ((nobs*sumnobs[_n-1])/(nobs+sumnobs[_n-1]) * (beta - mbeta[_n-1])^2 )) / (nobs + sumnobs[_n-1] - 1) ) if _n>1;

replace beta = mbeta;
replace se_beta = S;
replace nobs = sumnobs;

keep $onevars elast;
ds authors nobs elast, not;
collapse (last) `r(varlist)' nobs, by(authors elast);

by authors: gen belast = beta[_N];
by authors: gen selast = se_beta[_N];
drop if elast;

foreach k of global manyvars {;
    gen `k' = .;
};

replace tratio = abs(beta)/se_beta;
replace sig=tratio>=1.96;
replace pval = 2*normal(-tratio);

do metacode;
do make_labels;

/* THIS IS THE ARTICLE-LEVEL DATABASE */
save "$bpath/onesample.dta", replace;
